package com.nbot.server.controller;

import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.nbot.server.domain.AlarmpointDO;
import com.nbot.server.service.AlarmpointService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;

import com.nbot.server.util.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-06-24 23:48:50
 */

@Controller
@RequestMapping("/server/alarmpoint")
public class AlarmpointController {
    @Autowired
    private AlarmpointService alarmpointService;

    @GetMapping()
    @RequiresPermissions("server:alarmpoint:alarmpoint")
    String Alarmpoint() {
        return "server/alarmpoint/alarmpoint" ;
    }

    @ResponseBody
    @Log("查看")
    @GetMapping("/list")
    @RequiresPermissions("server:alarmpoint:alarmpoint")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AlarmpointDO> alarmpointList = alarmpointService.list(query);
        int total = alarmpointService.count(query);
        PageUtils pageUtils = new PageUtils(alarmpointList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:alarmpoint:add")
    String add() {
        return "server/alarmpoint/add" ;
    }

    @GetMapping("/edit/{autoid}")
    @RequiresPermissions("server:alarmpoint:edit")
    String edit(@PathVariable("autoid") Long autoid, Model model) {
            AlarmpointDO alarmpoint = alarmpointService.get(autoid);
        model.addAttribute("alarmpoint" , alarmpoint);
        return "server/alarmpoint/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:alarmpoint:add")
    public R save(AlarmpointDO alarmpoint) {
        if (alarmpointService.save(alarmpoint) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:alarmpoint:edit")
    public R update(AlarmpointDO alarmpoint) {
            alarmpointService.update(alarmpoint);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:alarmpoint:remove")
    public R remove( Long autoid) {
        if (alarmpointService.remove(autoid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:alarmpoint:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]autoids) {
            alarmpointService.batchRemove(autoids);
        return R.ok();
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public R importExcel(@RequestParam(value = "excelFile", required = false) MultipartFile file, HttpServletRequest request) {
        try {
            MultipartRequest multipartRequest=(MultipartRequest) request;
            MultipartFile excelFile=multipartRequest.getFile("excelFile");
            if(excelFile!=null){
                List<List<String>> datas = ExcelUtil.readXls(excelFile.getInputStream());
                //TODO: 读到的数据都在datas里面，根据实际业务逻辑做相应处理
                System.err.println("hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                if(datas!=null && datas.size()>0){
                    for(int i=0;i<datas.size();i++)
                    {
                        List<String> datai = datas.get(i);
                        int tempInt;
                        AlarmpointDO alarmpointSave = new AlarmpointDO();
                        AlarmpointDO alarmpointUpdate = new AlarmpointDO();
                        alarmpointSave.setGetdatapath(datai.get(0));//第1列，tag
                        alarmpointSave.setDispdesc(datai.get(1));//第2列，desc
                        tempInt = (int)Float.parseFloat(datai.get(2));//第3列，报警值（什么情况下报警） 0-1
                        alarmpointSave.setBoolt(tempInt+"");
                        tempInt = (int)Float.parseFloat(datai.get(3));//第4列，报警等级 1-2-3
                        alarmpointSave.setAlarmlevel(tempInt+"");
                        alarmpointSave.setAlarmclass(datai.get(4));//第5列，报警类别 安全类-生产类
                        alarmpointSave.setStationid(datai.get(5));//第6列，站号
                        alarmpointSave.setDevice(datai.get(6));//第7列，位置

                        alarmpointSave.setDatatype("zeroone");
                        alarmpointSave.setVf("0");
                        alarmpointSave.setVs("0");
                        alarmpointSave.setVt("0");
                        alarmpointSave.setTmp("0");

                        alarmpointUpdate = alarmpointService.getByTag(alarmpointSave.getGetdatapath());
                        if (alarmpointUpdate != null) {
                            System.err.println("alarmpiont update!");
                            alarmpointUpdate.setDispdesc(alarmpointSave.getDispdesc());
                            alarmpointUpdate.setBoolt(alarmpointSave.getBoolt());
                            alarmpointUpdate.setAlarmlevel(alarmpointSave.getAlarmlevel());
                            alarmpointUpdate.setAlarmclass(alarmpointSave.getAlarmclass());
                            alarmpointUpdate.setStationid(alarmpointSave.getStationid());
                            alarmpointUpdate.setDevice(alarmpointSave.getDevice());
                            alarmpointService.update(alarmpointUpdate);
                        }
                        else {
                            System.err.println("alarmpiont save!");
                            alarmpointSave.setAutoid((long)(alarmpointService.getMaxAutoid()+1));
                            alarmpointService.save(alarmpointSave);
                        }

                    }
                    return R.ok();
                }
                else{
                    return R.error();
                }
            }else{
                return R.error();
            }
        } catch (Exception e) {
            return R.error(-1,e.getMessage());
        }
    }

}
